package com.hk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.hk.annotation.Limit;
import com.hk.domain.dto.SubDateDTO;
import com.hk.domain.entity.User;
import com.hk.enums.LimitType;
import com.hk.service.business.CommonQueryBusiness;
import com.hk.service.business.UserBusiness;
import com.hk.service.demo.UserService;
import com.hk.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @RequiredArgsConstructor【Lombok下的功能】
 *   使用这个注解，然后要操作的对象定义为：private final xxx xxx;相当于通过构造器来DI，每一个参数代表一个对其他类的依赖。
 *   基于构造方法为属性赋值，容器通过调用类的构造方法将其进行依赖注入，从而替代了@Autowrited注解的功能
 */
@Api(tags = "【mybatis相关操作】")
@RestController
@RequestMapping("mybatis")
@RequiredArgsConstructor
public class MyBatisController {

    @Autowired
    private UserService userService;

//    @Autowired
    private final UserBusiness business;

//    @Autowired
    private final CommonQueryBusiness commonQueryBusiness;


    @ApiOperation("分页查询用户信息")
    @GetMapping("page")
    public R page(Page page) {
        IPage info = business.page(page);
        return R.ok().setData(info);
    }

    @GetMapping("test")
    public R testMyBatis() {
        List<User> allUser = userService.getAllUser();
        return R.ok().put("data", allUser);
    }

    @GetMapping("option")
    public R getUserByUserName(@RequestParam("username") String username) {
        User user = userService.queryUserByName(username);
        return R.ok().put("data", user);
    }

    @Limit(key = "customer_limit_test", period = 10, count = 3, limitType = LimitType.CUSTOMER)
    @GetMapping("other")
    public R queryUserHaveNull(@RequestParam("username") String username) {
        User user = userService.queryUserHaveNull(username);
        return R.ok().put("data", user);
    }

    @ApiOperation("删除缓存中的数据")
    @GetMapping("delete")
    public R deleteDate(@RequestParam("username") String username) {
        userService.deleteUserHaveNullData(username);
        return R.ok("删除成功~~");
    }


    @ApiOperation("公共查询数据")
    @GetMapping("commonQueryData")
    public R commonQueryData() {
        List<SubDateDTO> list = Lists.newArrayList(new SubDateDTO().setUsername("hkk").setPassword("1"), new SubDateDTO().setUsername("hk").setPassword("22222222222"));
        List<User> userList = commonQueryBusiness.getResult(list, UserService.class, new User());
        return R.ok().setData(userList);
    }

    @ApiOperation("通过Map删除数据")
    @GetMapping("remove")
    public R removeData(@RequestParam("username")String username,
                        @RequestParam("password")String password) {
        boolean result = userService.removeData(username, password);
        return R.ok(result ? "删除成功~" : "删除失败！");
    }

    @ApiOperation("手动操作事务-适用大事务的场景")
    @GetMapping("manualTransactions")
    public R manualTransactions() {
        return business.manualCommitTransaction();
    }


}
